import React from 'react';
import { StyleProp, Text as RnText, TextStyle, Platform } from 'react-native';
import { convert, bs } from './convert';
import { TTProps } from './Types';
export function TextHoc(style?: TTProps) {
  return function Text({ ...props }: TTProps) {
    Object.assign(props, style);
    const cStyle = bs(props);
    let textDecorationLine: 'underline' | 'line-through' | undefined;
    if (props.ulc) textDecorationLine = 'underline';
    if (props.dlc) textDecorationLine = 'line-through';
    const fStyle: StyleProp<TextStyle> = {
      color: props.c || '#060506',
      fontFamily: transFont(props.ff, props.fw) || 'Inter',
      fontWeight: transFw(props.ff, props.fw) || '500',
      fontSize: convert(props.fs || 16),
      lineHeight: convert(props.lh),
      textAlign: props.ta,
      textDecorationLine,
      textDecorationColor: props.ulc || props.dlc,
    };
    return (
      <RnText
        numberOfLines={+(props.nol ?? 1)}
        ellipsizeMode={props.em}
        allowFontScaling={false}
        {...props}
        style={[cStyle, fStyle, props.style]}
      />
    );
  };
}

export const Inter = TextHoc();
// GT字体在安卓上有bug，不能允许设置字重
export const GT = TextHoc({ ff: 'GT Flexa Trial', fw: '500' });
// Cool字体 设置字重无效，安卓设置自重过大会使字体失效
export const Cool = TextHoc({ ff: 'CoolveticaRg-Regular', fw: '500' });
//cool斜体
export const CoolItalic = TextHoc({ ff: 'CoolveticaRg-Italic' });
export const TT = TextHoc({ ff: 'TT Commons' });
// RD不能允许设置字重
export const RD = TextHoc({ ff: 'Raleway Dots', fw: '400' });
export function transFont(ff: TTProps['ff'], fw: TTProps['fw']) {
  let ffVal: TTProps['ff'] = ff;
  if (Platform.OS === 'android' && ff === 'TT Commons') {
    if (fw && (Number(fw) > 400 || fw === 'bold')) {
      ffVal = 'TT-Commons-Bold';
    } else {
      ffVal = 'TT-Commons-Regular';
    }
  }
  return ffVal;
}
export function transFw(ff: TTProps['ff'], fw: TTProps['fw']) {
  let fwVal: TTProps['fw'] = fw;
  if (
    Platform.OS === 'android' &&
    ff === 'TT Commons' &&
    ((fw && Number(fw) > 600) || fw === 'bold')
  ) {
    fwVal = '600';
  }
  return fwVal;
}
